package com.epin.config.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Description: shiro的配置类
 * 
 * @author fujiajun
 * 
 * @since 2017-07-28
 *
 */
@Configuration
public class ShiroConfig {


	/**
	 * <p>ShiroFilterFactoryBean 处理拦截资源文件问题。</p>
	 *
	 * @param securityManager 安全管理器
	 *
	 * @return ShiroFilterFactoryBean
	 */
	@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		
		// 设置全局的securityManager
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		
		return shiroFilterFactoryBean;
	}


	/**
	 * <p>配置securityManager Bean</p>
	 *
	 * @return SecurityManager
	 */
	@Bean
	public SecurityManager securityManager() {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		
		//设置登录realm.
	    securityManager.setRealm(this.shiroRealm());
	    
		return securityManager;
	}


	/**
	 * <p>用户登录realm</p>
	 *
	 * @return ShiroRealm
	 */
	@Bean
	public ShiroRealm shiroRealm() {
		return new ShiroRealm();
	}


	/**
	 *
	 * @param securityManager
	 *
	 * @return AuthorizationAttributeSourceAdvisor
	 */
	@Bean
	public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
		AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
		authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
		return authorizationAttributeSourceAdvisor;
	}


	/**
	 * <p>shiro缓存管理器;</p>
	 * <p>需要注入对应的其它的实体类中：</p>
	 * <p>1、安全管理器：securityManager</p>
	 * <p>可见securityManager是整个shiro的核心；</p>
	 *
	 * @return EhCacheManager
	 */
	@Bean
	public EhCacheManager ehCacheManager(){
		EhCacheManager cacheManager = new EhCacheManager();
		cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");
		return cacheManager;
	}


	/**
	 * <p>cookie对象;</p>
	 *
	 * @return SimpleCookie
	 */
	@Bean
	public SimpleCookie rememberMeCookie(){
		//这个参数是cookie的名称，对应前端的checkbox的name = rememberMe
		SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
		//记住我cookie生效时间30天 ,单位秒;
		simpleCookie.setMaxAge(259200);
		return simpleCookie;
	}


	/**
	 * <p>cookie管理对象;</p>
	 *
	 * @return CookieRememberMeManager
	 */
	@Bean
	public CookieRememberMeManager rememberMeManager(){
		CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
		cookieRememberMeManager.setCookie(rememberMeCookie());
		return cookieRememberMeManager;
	}


	/**
	 * <p>ShiroDialect，为了在thymeleaf里使用shiro的标签的bean</p>
	 *
	 * @return ShiroDialect
	 */
	@Bean
	public ShiroDialect shiroDialect(){
		return new ShiroDialect();
	}
}
